<script type="text/javascript" src="/SPDev/scripts/roboCAML.js"></script>
<script type="text/javascript" src="/SPDev/scripts/spUtils.unmin.js"></script>


<script type="text/javascript">
	//console.dir(SP);
	//debugger;

/***************************************
	Test 1 ~ getListItems - valid list
***************************************/

			spUtils.getListItems({
				//webUrl: ,
				listName : "Project Tasks",
				CAMLQuery: "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>45</Value></Eq></Where></Query></View>",
				//Folder,
				Include : ["Title", "RelatedIssues", "ID"],
				success : function( data, ctx ) {

					debugger;
					for ( var i = 0; i < data.length; i++ ) {
						var output = "{\n";
						
						for ( var staticName in data[ i ] ) {
							if ( data[ i ].hasOwnProperty( staticName ) ) {
								output += staticName + " : " + data[ i ][ staticName ] + "\n";
							}
						}
						
						output += "}";
						spUtils.log( output );
					}

					//debugger;
				},
				error : function( ctx, error ) {
					debugger;
				},
				debug : true
			});

/***************************************
	Test 2 ~ getListItems - invalid list
***************************************/
			spUtils.getListItems({
				//webUrl: ,
				listName : "InvalidList",
				//CAMLQuery,
				//Folder,
				Include : ["Title", "ID"],
				success : function( data, ctx ) {
					debugger;
					for ( var i = 0; i < data.length; i++ ) {
						var output = "{\n";
						
						for ( var staticName in data[ i ] ) {
							if ( data[ i ].hasOwnProperty( staticName ) ) {
								output += staticName + " : " + data[ i ][ staticName ] + "\n";
							}
						}
						
						output += "}";
						spUtils.log( output );
					}
				},
				error : function( ctx, error ) {
					debugger;
				},
				debug : true
			});


/***************************************
	Test 3 ~ getListItems - Wrapped with ExecuteOrDelayUntilScriptLoaded
***************************************/
			ExecuteOrDelayUntilScriptLoaded(
				function() {
					spUtils.getListItems({
						//webUrl: ,
						listName : "Project Tasks",
						//CAMLQuery,
						//Folder,
						Include : ["Title", "ID"],
						success : function( data, ctx ) {
							debugger;
							for ( var i = 0; i < data.length; i++ ) {
								var output = "{\n";
								
								for ( var staticName in data[ i ] ) {
									if ( data[ i ].hasOwnProperty( staticName ) ) {
										output += staticName + " : " + data[ i ][ staticName ] + "\n";
									}
								}
								
								output += "}";
								spUtils.log( output );
							}
							//debugger;
						},
						error : function( ctx, error ) {
							debugger;
						},
						debug : true
					});
				},
				'sp.js'
			);

/***************************************
	Test 4 ~ getListItems - Wrapped with $(document).ready() & ExecuteOrDelayUntilScriptLoaded
***************************************/
		$(document).ready(function() {
			ExecuteOrDelayUntilScriptLoaded(
				function() {
					spUtils.getListItems({
						//webUrl: ,
						listName : "Project Tasks",
						//CAMLQuery,
						//Folder,
						Include : ["Title", "ID"],
						success : function( data, ctx ) {
							debugger;
							for ( var i = 0; i < data.length; i++ ) {
								var output = "{\n";
								
								for ( var staticName in data[ i ] ) {
									if ( data[ i ].hasOwnProperty( staticName ) ) {
										output += staticName + " : " + data[ i ][ staticName ] + "\n";
									}
								}
								
								output += "}";
								spUtils.log( output );
							}
							//debugger;
						},
						error : function( ctx, error ) {
							debugger;
						},
						debug : true
					});
				},
				'sp.js'
			);
		});

/***************************************
	Test 5 ~ getListItems - Wrapped with ExecuteOrDelayUntilScriptLoaded & $(document).ready()
***************************************/
		ExecuteOrDelayUntilScriptLoaded(
			function() {
				$(document).ready(function() {
					spUtils.getListItems({
						//webUrl: ,
						listName : "Project Tasks",
						//CAMLQuery,
						//Folder,
						Include : ["Title", "ID"],
						success : function( data, ctx ) {

							debugger;
							for ( var i = 0; i < data.length; i++ ) {
								var output = "{\n";
								
								for ( var staticName in data[ i ] ) {
									if ( data[ i ].hasOwnProperty( staticName ) ) {
										output += staticName + " : " + data[ i ][ staticName ] + "\n";
									}
								}
								
								output += "}";
								spUtils.log( output );
							}
							//debugger;
						},
						error : function( ctx, error ) {
							debugger;
						},
						debug : true
					});
				});
			},
			'sp.js'
		);

/***************************************
	Test 6 ~ getListItems - Use ViewFields in CAMLQuery and Include: Should fail when ViewFields and Include have different columns
***************************************/
			spUtils.getListItems({
				//webUrl: ,
				listName : "Project Tasks",
				CAMLQuery : "<View><ViewFields><FieldRef Name='Title' /><FieldRef Name='ID' /></ViewFields><Query><Where><Neq><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Neq></Where></Query></View>",
				//Folder,
				Include : ["Title", "ID", "Author"],  //Fails when ViewFields and Include have different columns
				success : function( data, ctx ) {
				
					debugger;
					for ( var i = 0; i < data.length; i++ ) {
						var output = "{\n";
						
						for ( var staticName in data[ i ] ) {
							if ( data[ i ].hasOwnProperty( staticName ) ) {
								output += staticName + " : " + data[ i ][ staticName ] + "\n";
							}
						}
						
						output += "}";
						spUtils.log( output );
					}

					//debugger;
				},
				error : function( ctx, error ) {
					debugger;
				},
				debug : true
			});

/***************************************
	Test 7 ~ updateListItems - Updates 1 item based on ID
***************************************/
		spUtils.updateListItems({
			listName: "Project Tasks",
			id: 42,
			valuePairs: [ [ "Title", spUtils.isoDate() ] ]
		});

/***************************************
	Test 8 ~ updateListItems - Updates 2 items based on ID
***************************************/
		spUtils.updateListItems({
			listName : "Project Tasks",
			updates : {
				5 : { // the key is the item ID
					"Title" : spUtils.isoDate(),
					"Priority" : "(2) Normal"
				},
				6 : {
					"Title", spUtils.isoDate()
				}
			}
		});

/***************************************
	Test 9 ~ createListItems - Create an item
***************************************/
		spUtils.createListItems({
			listName : "Project Tasks",
			updates: [
				{
					Status : "Resolved",
					"RelatedProject{L}" : 1,
					Title: "Created by moi"
				}
			],
			success: function() { debugger; }
		});

/***************************************
	DEPRECATED ~ Test 9a ~ createListItems - Create an item
***************************************/
		spUtils.createListItems({
			listName : "Project Tasks",
			updates: [
				{
					valuePairs : [ [ "Priority", "(1) High"], [ "RelatedProject{L}", 1 ], [ "Title", "Created by moi" ] ]
				}
			],
			success: function() { debugger; }
		});

/***************************************
	Test 10 ~ createListItems - Create 2 items
***************************************/
	spUtils.createListItems({
		listName : "Project Tasks",
		updates: [
			{
				Priority : "(1) High",
				"RelatedProject{L}" : 1,
				Title: "Created in multitudes"
			},
			{
				Priority : "(2) Normal",
				"RelatedProject{L}" : 2, //Lookup, notice the suffix delimiter
				Title: "Created for us by us",
				Priority: "(1) High", //Choice
				V3Comments: "Saying something <b>important</b> here makes you feel <i>important!</i>", //Rich Text
				"RelatedIssues{L}": "3;#4" //MultiLookup, notice the ;# delimiter
			}
		],
		success: function() { debugger; }
	});
		
/***************************************
	DEPRECATED ~ Test 10a ~ createListItems - Create 2 items
***************************************/
		spUtils.createListItems({
			listName : "Project Tasks",
			updates: [
				{
					valuePairs : [ [ "Priority", "(2) Normal"], [ "RelatedProject{L}", 2 ], [ "Title", spUtils.isoDate() ] ]
				},
				{
					valuePairs : [
						[ "Priority", "(1) High" ],
						[ "RelatedProject{L}" /*Lookup, notice the suffix delimiter */, 1 ],
						[ "Title",  "Created for us by us" ],
						[ "V3Comments", "Saying something <b>important</b> here makes you feel <i>important!</i>"],
						[ "RelatedIssues{L}", "3;#4" /*MultiLookup, notice the ;# delimiter*/]
					]
				}
			],
			success: function() { debugger; }
		});
/***************************************
	Test 11 ~ updateListItems - Create an item ~ sytax sugar
***************************************/
		spUtils.updateListItems({
			op: "create",
			listName : "Project Tasks",
			updates: [
				{
					Status: "Resolved",
					"RelatedProject{L}" : 2,
					Title: "Created on" + spUtils.isoDate()
				}
			],
			success: function() { debugger; }
		});

/***************************************
	Test 12 ~ updateListItems - Create 2 items ~ syntax sugar
***************************************/
		spUtils.updateListItems({
			op: "create",
			listName : "Project Tasks",
			updates: [
				{
					Status: "Resolved",
					"RelatedProject{L}" : 1,
					Title: "Created in multitudes"
				},
				{
					Status: "Closed", //Choice
					"RelatedProject{L}" : 2, //Lookup, notice the suffix delimiter
					Title: "Created for us by us",
					Priority: "(1) High", //Choice
					V3Comments: "Saying something <b>important</b> here makes you feel <i>important!</i>", //Rich Text
					"RelatedIssues{L}": "3;#14" //MultiLookup, notice the ;# delimiter
				}
			],
			success: function() { debugger; }
		});

/***************************************
	Test 13 ~ deleteListItems - Delete 1 item
***************************************/
		spUtils.deleteListItems({
			listName : "Project Tasks",
			id: 12,
			success: function() { debugger; }
		});

/***************************************
	Test 14 ~ deleteListItems - Delete multiple items
***************************************/
		spUtils.deleteListItems({
			listName : "Project Tasks",
			id: [ 23, 24, 26, 28 ], // If item doesn't exist, an error will be thrown. The items processed prior to the error will still be modified accordingly.
			success: function() { debugger; }
		});

/***************************************
	Test 15 ~ updateListItems - Delete 1 item ~ a lil syntax sugar
***************************************/
		spUtils.updateListItems({
			listName : "Project Tasks",
			op: "delete",
			id: 1,
			success: function() { debugger; }
		});

/***************************************
	Test 16 ~ updateListItems - Delete multiple items ~ a lil syntax sugar
***************************************/
		spUtils.updateListItems({
			listName : "Project Tasks",
			op: "delete",
			id: [ 3, 4, 6, 8 ], // If item doesn't exist, an error will be thrown. The items processed prior to the error will still be modified accordingly.
			success: function() { debugger; }
		});


/***************************************
	Test 17 ~ createListItems - Create folder and modify leaf name
***************************************/
		spUtils.createListItems({
			listName : "Project Tasks",
			updates: [
				{
					folderUrl: "/SPDev/Lists/ListName",
					folderName: "folderName"
				}
			],
			success: function() { debugger; }
		});

/***************************************
	Test 18 ~ createListItems - Create folder, modify leaf name, and metadata.
***************************************/
		spUtils.createListItems({
			listName : "Project Tasks",
			updates: [
				{
					folderUrl: "/SPDev/Lists/ProjectTasks",
					folderName: "folder1",
					multiLinesHTML: "Saying something <b>important</b> here makes you feel <i>important!</i>", //Rich Text
					//personGroup: "dev\\administrator",
					chkBox: false,
					ol_EventAddress: "Event addy",
					ContentTypeId : "0x012000C77A544EBDFBD2419EE2F54FDAE1A9A200936BBDFC53359E4681BF0F0DC7DD3AE7", //FolderSorter CT Name
					URL: "http://google.com, Google"
				}
			],
			success: function() { debugger; }
		});


/***************************************
	Test 19 ~ createListItems - Create item based on CT.
***************************************/
		spUtils.createListItems({
			listName : "Project Tasks",
			updates: [
				{
					ContentTypeId : "0x01040004B13BF0D61E0D44A7D0327C51C01D8A", //Announcements CT
					Title: "It's my title and I'll type it when I want to."
				}
			],
			success: function() { debugger; }
		});

/***************************************
	Test 20 ~ addStatus - Create a status notification
***************************************/
	spUtils.addStatus("Does <span style='color:red;'>HTML</span> work in a status?", "yellow");

/***************************************
	Test 21 ~ addStatus/removeStatus - Create a status notification, then remove it.
***************************************/
/*
	var myStatus = spUtils.addStatus("Does <span style='color:red;'>HTML</span> work in a status?", "green"); The return id is async, so you'll need to use the callback method in order to time the removal correctly depending on how long your status needs to stay.
*/
	spUtils.addStatus("Does <span style='color:red;'>HTML</span> work in a status?", "green", function( statusId ) {
		spUtils.removeStatus( statusId, 2000 );
	});

/***************************************
	Test 22 ~ createListItems - Create item with people picker value.
***************************************/
		spUtils.createListItems({
			listName : "Project Tasks",
			updates: [
				{
					"AssignedTo{P}" : "DEV\\Administrator",
					Title: "Single Person/Group"
				}
			],
			success: function() { debugger; }
		});

/***************************************
	Test 23 ~ createListItems - Create item with multiple people picker values.
***************************************/
		spUtils.createListItems({
			listName : "Project Tasks",
			updates: [
				{
					folderName: "folder2People",
					ContentTypeId : "0x012000C77A544EBDFBD2419EE2F54FDAE1A9A200936BBDFC53359E4681BF0F0DC7DD3AE7",
					"personGroup{P}" : "DEV\\Administrator;#DEV\\SPUser1;#4",  //Will handle mixture of user IDs or the name of the user. Groups do not need domain prefix from what I can tell. User names need two slashes however. That may suck.
					Title: "Multiple People/Groups"
				}
			],
			success: function() { debugger; }
		});

/***************************************
	Test 24 ~ updateListItems - Updates 1 item based on ID, nested inside folder. Should work exactly as test#7
***************************************/
		spUtils.updateListItems({
			listName: "Project Tasks",
			id: 3,
			valuePairs: [ [ "Title", spUtils.isoDate() ], ["Priority", "(1) High" ] ],
			success: function() { debugger; }
		});

/***************************************
	Test 25 ~ updateListItems - Updates 2 items based on ID, nested inside folder. Should work exactly as test#8
***************************************/
	spUtils.updateListItems({
		listName : "Project Tasks",
		updates : {
			3 : { // the key is the item ID
				"Title" : spUtils.isoDate(),
				"Priority" : "(2) Normal",
				"AssignedTo{P}" : "DEV\\Administrator",
				_x0021__x0040__x0023__x0024__x00 : "weird static names work too. ;-)"
			},
			4 : { // the key is the item ID
				"Title" : spUtils.isoDate()
			},
			5 : {
				"Title" : spUtils.isoDate()
			}
		},
		success : function() { debugger; }
	});

/***************************************
	Test 26 ~ updateListItems - Updates item's lookup and people picker column
***************************************/
		spUtils.updateListItems({
			listName : "Project Tasks",
			updates : {
				111 : { // the key is the item ID
					"RelatedProject{L}" : spUtils.isoDate(), 
					"AssignedTo{P}" : 1
				}
			},
			success : function() { debugger; }
		});

		
/***************************************
	Test 27 ~ openModal
***************************************/	
	// Open an OOTB New form from the Project Tasks list
	spUtils.openModal({
		formType: "New",
		staticListName: "ProjectTasks"
	});
	
	// Open an external page in the modal
	spUtils.openModal({
		url: "http://iOnline247.me"
	});
	
	// Open the OOTB edit form for id: 42
	spUtils.openModal({
		staticListName: "ProjectTasks",
		formType: "Edit",
		id: 42
/*
		title: options.title || "",
		url: url,
		html: options.html || undefined,
		height: options.height || 600,
		width: options.width || 800,
		allowMaximize: options.allowMaximize || true,
		showMaximized: options.showMaximized || false,
		showClose: options.showClose || true,
		autoSize: options.autoSize || false,
		dialogReturnValueCallback: options.callback || onModalClose,
		//Use args to pass data to the modal.  Access using: window.frameElement.dialogArgs
		args: options.args || {}
*/
	});
	

	// Open custom form 
	spUtils.openModal({
		url: "/Site Pages/CustomForm.aspx?ID=42",
		title: "Mah super awesome custom form",
		height: 400,
		width: 700,
		allowMaximize: false,
		callback: function( dialogResult, returnValue ) {
			// Fires when dialog is closed
			var message = "YaY!!!!, opening a modal is now much easier. <br /><br /> Outcome of dialog = " + dialogResult;
			
			spUtils.notify( message, false );
		},
		//Use args to pass data to the modal.  Access using: window.frameElement.dialogArgs within the form that is being opened.
		args: {
			someDiv: $("#someDiv").html(),
			relatedStuff: $("#itemIds").text().split(";#")
		}
	});
	
/***************************************
	Test 28 ~ closeDialog - Closes the most recently opened modal.
***************************************/
	spUtils.closeDialog( SP.UI.DialogResult.OK /* SP.UI.DialogResult.OK === 1 */ );
	spUtils.closeDialog( SP.UI.DialogResult.Cancel /* SP.UI.DialogResult.Cancel === 0 */ );
	
/***************************************
	Test 29 ~ log() - Useful for debugging.
***************************************/
	// If console is open, will log there. If not, will fallback to alert();
	spUtils.log("I just can't do it captain...");
	
/***************************************
	Test 30 ~ notify - Provide feedback to user
***************************************/
	spUtils.notify("Isn't this spiffy?", true);
	/* 
		First parameter is the message
		Second parameter is optional and makes the notification persist or not.
	*/
	
/***************************************
	Test 31 ~ removeNotify - Removes notification. What else would it do? ;-)
***************************************/
	var userNotification = spUtils.notify("Isn't this spiffy?", true);
	
	setTimeout( function() {
		spUtils.removeNotify( userNotification );
	}, 15000);
	
/***************************************
	Test 32 ~ getListItems - Query multiple lists prior to making request to server...
***************************************/
	spUtils.getListItems({
		batch : [
			{
				//webUrl: ,
				listName : "Project Tasks",
				CAMLQuery: "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>45</Value></Eq></Where></Query></View>",
				//Folder,
				Include : ["Title", "RelatedIssues", "ID"]
			},
			{
				//webUrl: ,
				listName : "scripts",
				CAMLQuery: "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>59</Value></Eq></Where></Query></View>",
				//Folder,
				Include : ["Title", "ID"]
			}
		],
		success : function( data, ctx ) {
				debugger;
				for ( var i = 0; i < data.length; i++ ) {
					console.log( "RelatedIssues: " + data[ i ].RelatedIssues + "\n" + "ID: " + data[ i ].ID );
				}

				//debugger;
			},
			error : function( ctx, error ) {
				debugger;
			},
			debug : true
		});
			
			
/***************************************
	Test 33 ~ getListItems - From sub site, query root site.
***************************************/
	spUtils.getListItems({
		webUrl: "/",
		listName : "Style Library",
		CAMLQuery: "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>1</Value></Eq></Where></Query></View>",
		//Folder,
		Include : ["Name", "Modified", "Editor", "ID"],
		success : function( data, ctx ) {

			debugger;
			for ( var i = 0; i < data.length; i++ ) {
				console.log( "Name: " + data[ i ].Name + "\n" + "ID: " + data[ i ].ID );
			}

			//debugger;
		},
		error : function( ctx, error ) {
			debugger;
		},
		debug : true
	});
			
/*******************************************
TO DO ITEMS

3. Create tests for all other methods
4. Get startWorkflow working
5. Implement debug option for all CRUD options
********************************************/


/*******************************************
	spUtil Examples
*******************************************/

/******************************************
	Query List A, create list item in List B
******************************************/
	spUtils.getListItems({
		//webUrl: ,
		listName : "List A",
		CAMLQuery: "<View><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>99</Value></Eq></Where></Query></View>",
		//Folder,
		Include : ["Title", "RelatedIssues", "ID"],
		success : function( data, ctx ) {
			var updates = [];
			
			for ( var i = 0; i < data.length; i++ ) {
				var options = {
					valuePairs : []
				};
				
				console.log( "Title: " + data[ i ].Title + "\n" + "ID: " + data[ i ].ID );
				options.valuePairs.push( [ "Title", data[ i ].Title ] );
				
				updates.push( options );
			}
			
			debugger;
			
			spUtils.createListItems({
				listName : "List B",
				updates: updates,
				success: function() { debugger; }
			});
		},
		error : function( ctx, error ) {
			debugger;
		},
		debug : true
	});

</script>

<div id="showMe"></div>